﻿using MySql.Data.MySqlClient;
using Zadani09.util;
using Zadani09.db;


namespace Zadani09.db.DAO
{
    public abstract class AbstractDAO
    {

        protected UmGalDataContext db = null;
        protected MySqlConnection connection = null;

        public AbstractDAO()
        {
        }

        protected void initiateConnection()
        {
            connection = new MySqlConnection(Constants.MYSQL_CONNECTION_STRING);

            // tady ta db trida se mozna nemusi instancovat pro
            // kazde pripojeni a byla by jedna instance globalni,
            // zatim sem nezkousel instancovat ji bez toho parametru
            // ale mozna to pude
            
            // -azul-
            
            connection.Open();
            db = new UmGalDataContext(connection);
        }

        protected void closeConnection()
        {
            if (connection != null)
            {
                connection.Close();
                db = null; // GC
                // tady je nutne nullovat connection, close(); totiz neuvolnuje pripojeni
                // zpatky do poolu, takze se mi stalo nez sem to odhalil ze pool is empty :)
                connection = null;
            }
        }
    }
}
